import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/7 12:55
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.removeKdigits("10200", 3));
    }

    public String removeKdigits(String num, int k) {
        int n = num.length();
        if (n == k) {
            return "0";
        }
        StringBuffer sb = new StringBuffer();
        char[] A = num.toCharArray();
        sb.append(A[0]);
        for (int i = 1; i < n; i++) {
            while ( k > 0 && sb.length() >= 1 && A[i] < sb.charAt(sb.length() - 1)) {
                sb.deleteCharAt(sb.length() - 1);
                k--;
            }
            sb.append(A[i]);
            if (k == 0 && i == n - 1) {
                break;
            }
        }
        if (k > 0) {
            sb.delete(sb.length() - k, sb.length());
        }
        int i = 0;
        while (sb.length() > 0 && sb.charAt(i) == '0') {
            // 去除前导0
            sb.deleteCharAt(i);
        }
        // 全是0删除后成为空字符串
        return "".equals(sb.toString())  ? "0" : sb.toString();
    }
}
